/**
 * @filename BaseAction.java
 * @package com.tianjian.dass.action.system[MySSM]
 * @description 基础action,封装常用方法和属性
 * @author 胡正衛
 * @date 2012-6-3 下午12:02:38
 * @version v0.1
 */
package com.tianjian.common;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import net.sf.json.JSONArray;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;
import com.tianjian.common.logging.Logging;
import com.tianjian.oa.system.pojos.UserInfo;
import com.tianjian.util.GeneralUitl;

/**
 * @author 胡正衛
 */
public abstract class BaseAction extends ActionSupport {

	private static final long serialVersionUID = 3091271153990562205L;

	public static final String JSON = "json";

	/** UTF-8文字头. */
	protected byte[] UTF8_BOM = new byte[] { (byte) 0xef, (byte) 0xbb, (byte) 0xbf };

	/** 项目访问路径 */
	protected String basePath;

	protected HttpServletRequest request = ServletActionContext.getRequest();

	protected HttpServletResponse response = ServletActionContext.getResponse();

	protected HttpSession session = request.getSession();

	/** 当前登陆的用户 */
	protected UserInfo userinfo = (UserInfo) session.getAttribute(Config.SESSION_USERINFO);

	/** 用于存储JSON返回值 */
	protected Map<String, Object> result = new HashMap<String, Object>();

	/** 前端传来需要显示的页码,默认第1页. */
	protected int commpage = 1;

	/** 前端传来需要显示的行数,默认20行. */
	protected int commrows = 20;

	/** 前台传来的排序字段. */
	protected String commsort;

	/** 执行标识 */
	protected String exec;

	@Resource
	protected Logging logging;

	/***
	 * 该action的名称
	 */
	private String actionName = "";

	public BaseAction(String name) {
		this.actionName = name;
	}

	public abstract String get();

	public abstract String index();

	public abstract String view();

	public abstract String add();

	public abstract String edit();

	public abstract String del();

	/**
	 * 将request中的所有parameter数据放进criteria中.<br>
	 * 默认不允许空值插入<br>
	 * 默认不进行URL转码<br>
	 * 
	 * @param criteria
	 *            要注入的Criteria对象
	 */
	public Criteria inversionCriteria() {
		Criteria criteria = new Criteria();
		inversionCriteria(criteria, false, false);
		return criteria;
	}

	/**
	 * 将request中的所有parameter数据放进criteria中.<br>
	 * 默认不允许空值插入<br>
	 * 默认不进行URL转码<br>
	 * 
	 * @param criteria
	 *            要注入的Criteria对象
	 */
	public void inversionCriteria(Criteria criteria) {
		inversionCriteria(criteria, false, false);
	}

	/**
	 * 将request中的所有parameter数据放进criteria中.<br>
	 * 默认不进行URL转码<br>
	 * 
	 * @param criteria
	 *            要注入的Criteria对象
	 * @param allowedNull
	 *            是否允许将空值放如criteria对象中
	 */
	public void inversionCriteria(Criteria criteria, boolean allowedNull) {
		inversionCriteria(criteria, allowedNull, false);
	}

	/**
	 * 将request中的所有parameter数据放进criteria中.
	 * 
	 * @param criteria
	 *            要注入的Criteria对象
	 * @param allowedNull
	 *            是否允许将空值放如criteria对象中
	 * @param urlDecode
	 *            是否要进行url解码
	 */
	public void inversionCriteria(Criteria criteria, boolean allowedNull, boolean urlDecode) {
		Enumeration<?> url = request.getParameterNames();
		if (url != null) {
			while (url.hasMoreElements()) {
				String _pare = url.nextElement().toString();
				String _val = "";
				_val = request.getParameter(_pare);
				if (_val != null) {
					_val = _val.trim();
					if (!_val.equals("") && !_val.equals("undefined")) { // 将空值去掉
						if (urlDecode) { // 转码
							try {
								_val = java.net.URLDecoder.decode(_val, "UTF-8");
								// _val=new
								// String(_val.getBytes("iso-8859-1"),"UTF-8");
							} catch (UnsupportedEncodingException e) {
								e.printStackTrace();
							}
						}
						criteria.put(_pare, _val);
					} else if (allowedNull) { // 不去掉空值
						criteria.put(_pare, _val);
					}
				}
			}
		}
		criteria.setCommstart((commpage - 1) * commrows);
		criteria.setCommend(commpage * commrows);
		criteria.setCommsort(commsort);
	}

	public HttpServletRequest getRequest() {
		return request;
	}

	public HttpServletResponse getResponse() {
		return response;
	}

	public HttpSession getSession() {
		return session;
	}

	/**
	 * 向页面输出内容
	 * 
	 * @param object
	 *            要输出的内容
	 */
	public void print(Object object) {
		print(object, "text/html", false);
	}

	/**
	 * 向页面输出内容
	 * 
	 * @param object
	 *            要输出的内容
	 * @param contentType
	 *            输出内容类型 如 xml, text, html等
	 */
	public void print(Object object, String contentType) {
		print(object, contentType, false);
	}

	/**
	 * 向页面输出内容
	 * 
	 * @param object
	 *            要输出的内容
	 * @param contentType
	 *            输出内容类型 如 xml, text, html等
	 * @param utf8Bom
	 *            是否添加utf-8标识, text，html方式下不能使用
	 */
	public void print(Object object, String contentType, boolean utf8Bom) {
		try {
			response.setContentType(contentType);
			response.setCharacterEncoding("UTF-8");
			response.setHeader("Cache-Control", "no-cache");
			if (utf8Bom) {
				String bom = new String(UTF8_BOM, "UTF-8");
				response.getWriter().print(bom + object);
			} else {
				response.getWriter().print(object);
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	/**
	 * 获取[项目访问路径]
	 * 
	 * @return 项目访问路径
	 */
	public String getBasePath() {
		if (basePath == null) {
			/* 设置项目访问路径. */
			if (basePath == null) {
				basePath = GeneralUitl.GetRealURL(request);
			}
		}
		return basePath;
	}

	
	/**
	 * 获取对象Json.
	 * 
	 * @param obj
	 *            list对象
	 * @return json串
	 */
	public String getStringJson(Object obj) {
		JSONArray json = JSONArray.fromObject(obj);
		return json.toString().replace("\"", "\\\"");
	}

	/**
	 * 获取[该action的名称]
	 * @return 该action的名称
	 */
	public String getActionName() {
		return actionName;
	}

	/**
	 * @return the 当前登陆的用户
	 */
	public UserInfo getUserinfo() {
		return userinfo;
	}

	/**
	 * 获取[用于存储JSON返回值]
	 * @return 用于存储JSON返回值
	 */
	public Map<String, Object> getResult() {
		return result;
	}

	/**
	 * 设置[前端传来需要显示的页码默认第1页.]
	 * @param page
	 */
	public void setCommpage(int commpage) {
		this.commpage = commpage;
	}

	/**
	 * 设置[前端传来需要显示的行数默认20行.]
	 * @param rows
	 */
	public void setCommrows(int commrows) {
		this.commrows = commrows;
	}

	/**
	 * 设置[前端传来需要需要排序的字段.]
	 * @param rows
	 */
	public void setCommsort(String commsort) {
		if (commsort == null || commsort.length() == 0) return;
		commsort = commsort.replaceAll("'", "");
		commsort = commsort.replaceAll("\\*", "");
		commsort = commsort.replaceAll("\\sor\\s", "");
		commsort = commsort.replaceAll("\\sselect\\s", "");
		commsort = commsort.replaceAll("\\sfrom\\s", "");
		commsort = commsort.replaceAll("\\swhere\\s", "");
		commsort = commsort.replaceAll("\\supdate\\s", "");
		commsort = commsort.replaceAll("\\sdelete\\s", "");
		commsort = commsort.replaceAll("\\sdelete\\s", "");
		if (commsort == null || commsort.length() == 0) return;
		this.commsort = commsort;
	}

	/**
	 * 设置[执行标识]
	 * @param exec
	 */
	public void setExec(String exec) {
		this.exec = exec;
	}

}
